﻿using System;
using System.Net;
using System.Net.Mail;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Paltop_DatabaseLayer;
using System.Text;
using System.Security.Cryptography;
using System.Text.RegularExpressions;
using Paltop_v0._1.Entities;

namespace Paltop_v0._1.UI
{
    public partial class Desktop : System.Web.UI.Page
    {
        public string StickyNotes = "";
        public int numberOfSNotes = 0;
        protected void Page_Load(object sender, EventArgs e)
        {

            try
            {
                Paltop_.Controllers.UserControler account = new Paltop_.Controllers.UserControler();
                Paltop_.Controllers.StikyNoteController sticky = new Paltop_.Controllers.StikyNoteController();
                int accountID = Convert.ToInt32(Session["ACCOUNTID"].ToString());
                Account test = account.getAccountData(accountID);
                user.Text = test.Name;
                StickyNotes = sticky.StickyNoteReturnCreated(int.Parse(Session["ACCOUNTID"].ToString()), ref numberOfSNotes);//2=
                UserLog();
            }
            catch (Exception ex)
            {
                Response.Redirect("~/Startup.aspx");
            }
        }

        protected void LogoutClick(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            Session.Abandon();
            Response.Redirect("~/Startup.aspx");
        }
        protected void UserLog()
        {
            List<Os> OSNames=(List<Os>)Application["osTypes"];
            List<Browser> BrowserNames = (List<Browser>)Application["browserTypes"];
            List<Resolution> ResolutionNames = (List<Resolution>)Application["resTypes"];
            String os = Request.Browser.Platform;
            os = os.Substring(0, 3).ToLower();
            String Browser = Request.Browser.Browser.ToLower();
            Paltop_DatabaseLayer.PaltopDatabaseEntities db = new Paltop_DatabaseLayer.PaltopDatabaseEntities();
            var oslist = OSNames.Where(p => p.OSName.Contains(os)).FirstOrDefault();
            var browserlis = BrowserNames.Where(p => p.BrowserName.Contains(Browser)).FirstOrDefault();
            Paltop_DatabaseLayer.SYSUserLog user = new Paltop_DatabaseLayer.SYSUserLog();
 
            user.Date = DateTime.Now;
            user.BrowserVersion = Request.Browser.MajorVersion;
            user.OSVersion = 0;
            user.CountryID = 2;
            string h = Session["HIEGHT"].ToString();
            string w = Session["WIDTH"].ToString();
            Int32 hieght = new Int32();
            Int32 wieght = new Int32();
            if (h != null)
            {
                hieght = Convert.ToInt32(h);
            }
            if (w != null)
            {
                wieght = Convert.ToInt32(w);
            }
            var res = ResolutionNames.Where(p => p.Height == hieght && p.Width == wieght).First();
            if (res != null)
                user.ResolutionID = res.ResolutionID;
            else
                user.ResolutionID = 7;
            if (oslist == null)
                user.OSID = OSNames.Last().OSID;
            else
                user.OSID = oslist.OSID;
            if (browserlis == null)
                user.BrowserID = BrowserNames.Last().BrowserID;
            else
                user.BrowserID = browserlis.BrowserID;
            db.SYSUserLogs.AddObject(user);
            db.SaveChanges();
        }
    }
}